Skip to content

feat: malformed tool call recovery for open-weight models#12231

Draft
roomote-v0[bot] wants to merge 1 commit intomainfrom
feature/malformed-tool-call-recovery
Draft

feat: malformed tool call recovery for open-weight models#12231
roomote-v0[bot] wants to merge 1 commit intomainfrom
feature/malformed-tool-call-recovery

Conversation

@roomote-v0
Copy link
Copy Markdown
Contributor

@roomote-v0 roomote-v0 Bot commented Apr 29, 2026

Related GitHub Issue

Closes: #12185

Description

This PR attempts to address Issue #12185. Feedback and guidance are welcome.

When models (especially open-weight models like qwen3-coder via OpenAI-compatible APIs) emit tool calls as XML markup in their text output instead of using native tool calling, Roo now detects these patterns and provides specific feedback in the retry message so the model can self-correct.

Key implementation details:

  1. New utility (src/core/task/malformed-tool-call-recovery.ts): Detects common malformed XML-style tool call patterns in assistant text output, including <function=TOOL_NAME><parameter=PARAM_NAME>VALUE</parameter></function> (with or without trailing </tool_call>) and XML-style <tool_name><param>value</param></tool_name>.

  2. Enhanced error message (src/core/prompts/responses.ts): formatResponse.noToolsUsed() now accepts optional malformed tool call info. When detected, the retry message tells the model exactly what XML pattern it tried to use and instructs it to use native tool calling instead.

  3. Integration (src/core/task/Task.ts): In the !didToolUse code path, before sending the retry message, we scan assistantMessage for malformed patterns and include recovery info if found.

Design choices:

  • Non-invasive: only activates in the "no native tool use detected" path
  • Safe: never auto-executes recovered tool calls
  • Recovered info is used solely to provide better retry guidance to the model
  • XML pattern 2 requires underscore in tag names to avoid matching regular HTML

Test Procedure

  • 13 unit tests added in src/core/task/__tests__/malformed-tool-call-recovery.spec.ts
  • Tests cover: function-style patterns, trailing </tool_call>, <tool_call> wrappers, multiple parameters, surrounding text, XML-style patterns, and no-match edge cases (plain text, empty string, regular HTML)
  • Run: cd src && npx vitest run core/task/__tests__/malformed-tool-call-recovery.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes.
  • Documentation Impact: No documentation updates required (internal recovery mechanism).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

No documentation updates needed. This is an internal recovery mechanism that improves model retry behavior without changing any user-facing API or configuration.

Interactively review PR in Roo Code Cloud

When models (especially open-weight models via OpenAI-compatible APIs)
emit tool calls as XML markup in their text output instead of using
native tool calling, Roo now detects these patterns and provides
specific feedback in the retry message.

This is non-invasive: it only activates when no native tool calls
were detected, and never auto-executes recovered tool calls. The
recovered information is used solely to give the model a clearer
retry message so it can self-correct.

Closes #12185
// Pattern 2: XML-style <tool_name><param_name>value</param_name></tool_name>
// Common with some models that try to emulate XML tool calling.
// Requires at least one underscore in the tool name to avoid matching regular HTML tags.
const xmlToolPattern = /<([a-z]+_[a-z_]+)>\s*((?:<[a-z_]+>[\s\S]*?<\/[a-z_]+>\s*)+)<\/\1>/i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Roo should allow recovery when model returns malformed tool call XML

2 participants